package com.example.hospitalmanagement

import android.app.Application
import android.util.Log
import com.example.hospitalmanagement.http.IChargeRequest
import com.example.hospitalmanagement.http.IDrugPriceRequest
import com.example.hospitalmanagement.http.IHospitalRequest
import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

class BaseApplication:Application() {
    companion object{
        lateinit var iDrugPriceRequest: IDrugPriceRequest
        lateinit var iHospitalRequest: IHospitalRequest
        lateinit var iChargeRequest: IChargeRequest
        private const val TAG = "BaseApplication"
    }

    override fun onCreate() {
        super.onCreate()
        val retrofit= Retrofit.Builder()
            .baseUrl("http://192.168.31.214:8080/")
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(OkHttpClient.Builder().addInterceptor{
                val request=it.request()
                val response=it.proceed(request)
                val result = response.body()?.string()
                Log.e(TAG,result+"")
                response.newBuilder()
                    .body(ResponseBody.create(response.body()?.contentType(),result))
                    .build()
            }.build())
            .build()
        iHospitalRequest=retrofit.create(IHospitalRequest::class.java)

        val chargeRetrofit= Retrofit.Builder()
            .baseUrl("http://192.168.31.110:8080/")
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(OkHttpClient.Builder().addInterceptor{
                val request=it.request()
                val response=it.proceed(request)
                val result = response.body()?.string()
                Log.e(TAG,result+"")
                response.newBuilder()
                    .body(ResponseBody.create(response.body()?.contentType(),result))
                    .build()
            }.build())
            .build()
        iChargeRequest=chargeRetrofit.create(IChargeRequest::class.java)
        val drugRetrofit= Retrofit.Builder()
            .baseUrl("http://192.168.31.158:8080/")
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(OkHttpClient.Builder().addInterceptor{
                val request=it.request()
                val response=it.proceed(request)
                val result = response.body()?.string()
                Log.e(TAG,result+"")
                response.newBuilder()
                    .body(ResponseBody.create(response.body()?.contentType(),result))
                    .build()
            }.build())
            .build()
        iDrugPriceRequest=drugRetrofit.create(IDrugPriceRequest::class.java)
    }
}